var {
  randomUtils,
  ocrUtils,
  clickUtils,
  timeUtils,
  clickContent,
} = require("../../utils/index");
var { Action } = require("../../base/index");
var pointMap = require("./pointMap");

var aPaiShe = new Action("去拍摄", {
  maxRunSCount: 1,
  maxRunFCount: 3,
});

function waitUtil(content, maxTime) {
  maxTime = maxTime || 60;
  var startTime = Date.now();
  while (true) {
    sleep(5 * 1000);
    if (clickContent(content)) {
      break;
    }
    var hasExec = timeUtils.hasExec(startTime);
    if (hasExec >= maxTime) {
      break;
    }
  }
}

aPaiShe.exec = function () {
  var content = "去拍摄";
  var results = ocrUtils.scrollAndFindSome(content, false, false, 5);
  if (results.length > 0) {
    var TextOCR = results[0];
    clickUtils.clickTextOCR(TextOCR);
    sleep(randomUtils.getRandom(2, 3, 1000));
    clickUtils.clickPointMap(pointMap.拍摄.点击拍摄);
    aPaiShe.startWait();
  } else {
    this.runFCount++;
  }
};
aPaiShe.startWait = function () {
  sleep(randomUtils.getRandom(2, 3, 1000));
  waitUtil("下一步");
  sleep(randomUtils.getRandom(2, 3, 1000));
  if (clickContent("发布")) {
    waitUtil("返回领奖", 60);
    sleep(randomUtils.getRandom(1.5, 3, 1000));
    if (clickContent("开心收下")) {
      this.runSCount++;
    }
  } else {
    this.runFCount++;
  }
};

module.exports = aPaiShe;
